﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.BLL;
using Giaiphongmatbang.DataAccess;
using System.Text;
using System.IO;

namespace Giaiphongmatbang
{
    public partial class CostEstimate : System.Web.UI.Page
    {
        int ProjectID
        {
            get { return (int)ViewState["ProjectID"]; }
            set { ViewState["ProjectID"] = value; }
        }

        GPMBDataContext db = new GPMBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string projectName = string.Empty;
                try
                {
                    ProjectID = int.Parse(Request.QueryString["pID"]);
                    Project project = db.Projects.FirstOrDefault(p => p.ProjectID == ProjectID);
                    projectName = project.ProjectName;
                }
                catch { ProjectID = 0; }
                Title = ":: Tính dự toán chi phí - Dự án " + projectName + " ::";
                BindData();                
            }
        }

        int Max(params int?[] param)
        {
            int max = param[0].Value;
            foreach (var item in param)
            {
                if (item.Value > max) max = item.Value;
            }
            return max;
        }

  
        protected void BindData()
        {
            ltrBindContent.Text = new Exports().getEstimateByProjectID(ProjectID);
        }
        protected void Printing(string file_extended)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/csv";
            Response.Charset = "";
            Response.AddHeader("Content-Disposition", "attachment;filename=baocao" + file_extended);
            Response.ContentEncoding = Encoding.Unicode; 
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            Response.Output.Write( Utility.Utilities.MSWordHeader("A3",true) + new Exports().getEstimateByProjectID(ProjectID) + Utility.Utilities.MSWordFooter());
            Response.Flush();
            Response.End();
        }

        protected void btnExportToWord_Click(object sender, EventArgs e)
        {
            Printing(".doc");
        }
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            Printing(".xls");
        }

    }
}